package worker

import (
	"fmt"
	"strings"
	"wf-deploy-agent/cluster"
	"wf-deploy-agent/cmd"
	"wf-deploy-agent/conf"
	"wf-deploy-agent/consts"
	"wf-deploy-agent/infra"
	"wf-deploy-agent/notify"
	"wf-deploy-agent/util"
)

// Cluster 集群事件处理程序
type Cluster struct {
}

func (c *Cluster) process() {
	err := notify.Subscribe(conf.CF.Kafka.ClusterTopic, fmt.Sprintf("agent-%s:%d",
		util.GetLocalIp(),
		conf.CF.Agent.Service.Port),
		c.handle, c.post)
	if err != nil {
		infra.Logger.Panic(err)
	}
}

func (c *Cluster) handle(command *cmd.Command) error {
	var err error
	opType := strings.ToUpper(command.OpType)
	switch opType {
	case consts.SyncPlugin:
		cluster.EnableSyncPlugin()
		cluster.SyncPlugin()
		break
	}

	return err
}

func (c *Cluster) post(command *cmd.Command, err error) error {
	return nil
}
